package com.yricky.memoria.ui.component.common

import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.interaction.collectIsFocusedAsState
import androidx.compose.foundation.interaction.collectIsPressedAsState
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentWidth
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.Delete
import androidx.compose.material.icons.rounded.Edit
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.unit.dp
import com.yricky.memoria.R
import com.yricky.memoria.utils.RStr

/**
 * @author Yricky
 * @date 2021/12/10
 */

// 是否要单独分一个文件

object MenuItems{
    @Composable
    fun Item(state: MutableState<Boolean>, icon: ImageVector? = null, text:String? = null, callback:()->Unit){
        val interactionSource = remember { MutableInteractionSource() }
        val pressState = interactionSource.collectIsPressedAsState()
        val focusState = interactionSource.collectIsFocusedAsState()
        DropdownMenuItem(
            modifier = Modifier
                .wrapContentWidth(),
            onClick = {
                state.value = false
                callback()
            },
            interactionSource = interactionSource
        ) {
            icon?.let { icon ->
                Icon(
                    imageVector = icon,
                    contentDescription = text,
                    modifier = Modifier.width(icon.defaultWidth),   // 若不加这一行会导致宽度异常
                    tint =
                    if(pressState.value || focusState.value)
                        MaterialTheme.colors.primary
                    else
                        LocalContentColor.current.copy(alpha = LocalContentAlpha.current)
                )
            }
            text?.let { text ->
                Text(
                    text = text,
                    maxLines = 1,
                    softWrap = false,
                    modifier = Modifier
                        .wrapContentWidth()
                        .padding(start = 8.dp, end = 4.dp),
                    color =
                    if(pressState.value || focusState.value)
                        MaterialTheme.colors.primary
                    else
                        LocalContentColor.current.copy(alpha = LocalContentAlpha.current)
                )
            }
        }
    }
    @Composable
    fun Rename(state: MutableState<Boolean>,callback:()->Unit){
        Item(
            state = state,
            icon = Icons.Rounded.Edit,
            text = RStr.from(R.string.rename),
            callback = callback
        )
    }

    @Composable
    fun Edit(state: MutableState<Boolean>,callback:()->Unit){
        Item(
            state = state,
            icon = Icons.Rounded.Edit,
            text = RStr.from(R.string.edit),
            callback = callback
        )
    }

    @Composable
    fun Delete(state: MutableState<Boolean>,callback:()->Unit){
        Item(
            state = state,
            icon = Icons.Rounded.Delete,
            text = RStr.from(R.string.delete),
            callback = callback
        )
    }
}
